﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiMovProcessTreeGridWrapper
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui48;


namespace DnProcessSimulateCommands;

internal class CUiMovProcessTreeGridWrapper : TxFlexGridExtension
{
  private const int OPERATIONS_NAME_COL_NUM = 0;
  private const int OPERATION_SELECTION_INDICATION_COL_NUM = 1;
  private const int OPERATION_MOVIE_INDICATION_COL_NUM = 2;
  private ArrayList m_slelectedRowDataInProcessTree;
  private ContextMenu m_processTreeContextMenu;
  private ImageList m_imageListAVIIcon;
  private ContextMenu m_contextMenuMarkByLavel;
  private IContainer components;
  private CellStyle m_csGrayedCenterItem;
  private string m_readOnlyTooltip = "";
  private ToolTip m_toolTip;
  private int m_previousMouseRow = -1;

  public ArrayList SelectedRowData => this.m_slelectedRowDataInProcessTree;

  internal CUiMovProcessTreeGridWrapper()
  {
    this.InitializeComponent();
    this.m_slelectedRowDataInProcessTree = new ArrayList();
    this.CreateToolTip();
  }

  protected virtual void SetupCustomStyles()
  {
    ((TxFlexGrid) this).SetupCustomStyles();
    this.m_csGrayedCenterItem = ((C1FlexGridBase) this).Styles.Add("GrayedCenter");
    this.m_csGrayedCenterItem.BackColor = SystemColors.ControlDark;
    this.m_csGrayedCenterItem.Border.Color = SystemColors.Window;
    this.m_csGrayedCenterItem.TextAlign = (TextAlignEnum) 4;
    this.m_csGrayedCenterItem.ImageAlign = (ImageAlignEnum) 4;
  }

  private void CreateToolTip()
  {
    this.m_readOnlyTooltip = this.GetResourceManager().GetString("MOVIE_MANAGER_READONLY_TOOLTIP");
    this.m_toolTip = new ToolTip();
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    ((TxFlexGrid) this).Dispose(disposing);
  }

  private void InitializeComponent()
  {
    this.components = (IContainer) new Container();
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMovProcessTreeGridWrapper));
    this.m_contextMenuMarkByLavel = new ContextMenu();
    this.m_processTreeContextMenu = new ContextMenu();
    this.m_imageListAVIIcon = new ImageList(this.components);
    ((ISupportInitialize) this).BeginInit();
    ((Control) this).SuspendLayout();
    this.m_imageListAVIIcon.ImageStream = (ImageListStreamer) componentResourceManager.GetObject("m_imageListAVIIcon.ImageStream");
    this.m_imageListAVIIcon.TransparentColor = Color.Transparent;
    this.m_imageListAVIIcon.Images.SetKeyName(0, "");
    this.m_imageListAVIIcon.Images.SetKeyName(1, "");
    componentResourceManager.ApplyResources((object) this, "$this");
    ((C1FlexGridBase) this).ExtendLastCol = true;
    ((C1FlexGridBase) this).SelectionMode = (SelectionModeEnum) 7;
    ((C1FlexGridBase) this).StyleInfo = componentResourceManager.GetString("$this.StyleInfo");
    ((Control) this).MouseMove += new MouseEventHandler(this.CUiMovProcessTreeGridWrapper_MouseMove);
    ((C1FlexGridBase) this).AfterEdit += new RowColEventHandler(this.CUiMovProcessTreeGridWrapper_AfterEdit);
    ((C1FlexGridBase) this).SelChange += new EventHandler(this.CUiMovProcessTreeGridWrapper_SelChange);
    ((ISupportInitialize) this).EndInit();
    ((Control) this).ResumeLayout(false);
  }

  public ArrayList GetAllData()
  {
    ArrayList allData = new ArrayList();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0)
      {
        CApMovMovieManagerNodeData userData = ((RowCol) row).UserData as CApMovMovieManagerNodeData;
        allData.Add((object) userData);
      }
    }
    return allData;
  }

  public ArrayList GetAllMarkedData()
  {
    ArrayList allMarkedData = new ArrayList();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0)
      {
        CApMovMovieManagerNodeData userData = ((RowCol) row).UserData as CApMovMovieManagerNodeData;
        if (userData.IsNodeOperationSelected)
          allMarkedData.Add((object) userData);
      }
    }
    return allMarkedData;
  }

  public void InitializeProcessTreeGrid(CApMovMovieManagerApp movieManager)
  {
    this.CreateContectMenu();
    this.LoadTreeData(movieManager.CreateOperationTreeNodes());
  }

  public void ReloadAllProcessTreeNodes()
  {
    ArrayList arrayList = new ArrayList();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0)
      {
        CApMovMovieManagerNodeData userData = ((RowCol) row).UserData as CApMovMovieManagerNodeData;
        userData.InitializeAllAttachments();
        if (userData.IsNodeHasAVIAttachments)
          ((C1FlexGridBase) this).SetCellImage(((RowCol) row).Index, 2, (Image) this.GetAviFileicon());
        else
          ((TxFlexGrid) this).SetCellImage(((RowCol) row).Index, 2, (TxImage) null, new Size());
      }
    }
  }

  public void ReloadAllProcessTreeNodes(int rowIndex)
  {
    if (rowIndex <= 0)
      return;
    if ((((RowCol) ((C1FlexGridBase) this).Rows[rowIndex]).UserData as CApMovMovieManagerNodeData).IsNodeHasAVIAttachments)
      ((C1FlexGridBase) this).SetCellImage(rowIndex, 2, (Image) this.GetAviFileicon());
    else
      ((TxFlexGrid) this).SetCellImage(rowIndex, 2, (TxImage) null, new Size());
  }

  protected virtual void ClearAllMarked()
  {
    this.m_slelectedRowDataInProcessTree.Clear();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && row.Node.Key != null)
      {
        for (int index = 1; index < ((RowColCollection) ((C1FlexGridBase) this).Cols).Count; ++index)
        {
          if (!(row.Node.Key as CApMovMovieManagerNodeData).CanAttachNewObject && 1 == index)
            ((C1FlexGridBase) this).SetCellStyle(((RowCol) row).Index, index, this.m_csGrayedCenterItem);
        }
      }
    }
  }

  protected virtual void MarkAllSelected()
  {
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows.Selected)
    {
      if (((RowCol) row).Index > 0 && row.Node.Key != null)
        this.m_slelectedRowDataInProcessTree.Add((object) (row.Node.Key as CApMovMovieManagerNodeData));
    }
  }

  private void CUiMovProcessTreeGridWrapper_SelChange(object sender, EventArgs e)
  {
    this.UpdateSelectedRows();
  }

  private void CUiMovProcessTreeGridWrapper_AfterEdit(object sender, RowColEventArgs e)
  {
    this.UpdateDataInCell(e.Col, e.Row);
  }

  private void CUiMovProcessTreeGridWrapper_MouseMove(object sender, MouseEventArgs e)
  {
    int mouseCol = ((C1FlexGridBase) this).MouseCol;
    int mouseRow = ((C1FlexGridBase) this).MouseRow;
    if (mouseCol == 1 && mouseRow > 0)
    {
      if (((RowCol) ((C1FlexGridBase) this).Rows[mouseRow]).UserData is CApMovMovieManagerNodeData userData && !userData.CanAttachNewObject)
      {
        string readOnlyTooltip = this.m_readOnlyTooltip;
        if (!(readOnlyTooltip != this.m_toolTip.GetToolTip((Control) this)))
          return;
        this.m_toolTip.SetToolTip((Control) this, readOnlyTooltip);
        this.m_previousMouseRow = mouseRow;
      }
      else
      {
        if (this.m_previousMouseRow == mouseRow)
          return;
        this.m_toolTip.RemoveAll();
      }
    }
    else
      this.m_toolTip.RemoveAll();
  }

  private void UpdateDataInCell(int col, int row)
  {
    if (col != 1)
      return;
    this.UpdateAssignmentChanged(row);
  }

  private void UpdateAssignmentChanged(int rowIndex)
  {
    if (!(((C1FlexGridBase) this).Rows[rowIndex].Node.Key is CApMovMovieManagerNodeData key))
      return;
    bool flag = ((C1FlexGridBase) this).GetCellCheck(rowIndex, 1) == 1;
    if (key.IsNodeOperationSelected == flag)
      return;
    key.IsNodeOperationSelected = flag;
  }

  public void ExpandTreeToCurrentLavel(int selectedLevel)
  {
    this.ExpandOnlyCollapsedNodes(selectedLevel);
  }

  private void ExpandOnlyCollapsedNodes(int selectedLevel)
  {
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && row.IsNode && row.Node.Level == selectedLevel - 1 && row.Node.Collapsed)
        row.Node.Expanded = true;
    }
  }

  private void CreateContectMenu()
  {
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    MenuItem menuItem3 = new MenuItem();
    MenuItem menuItem4 = new MenuItem();
    MenuItem menuItem5 = new MenuItem();
    this.m_contextMenuMarkByLavel.MenuItems.AddRange(new MenuItem[5]
    {
      menuItem1,
      menuItem2,
      menuItem3,
      menuItem4,
      menuItem5
    });
    ResourceManager resourceManager = this.GetResourceManager();
    menuItem1.Index = 0;
    menuItem1.Text = resourceManager.GetString("MOVIE_MANAGER_CONTEXT_MENU_MARKALL");
    menuItem2.Index = 1;
    menuItem2.Text = resourceManager.GetString("MOVIE_MANAGER_CONTEXT_MENU_MARKONLY");
    menuItem3.Index = 2;
    menuItem3.Text = resourceManager.GetString("MOVIE_MANAGER_CONTEXT_MENU_MARKCHILDSONLY");
    menuItem4.Index = 3;
    menuItem4.Text = resourceManager.GetString("MOVIE_MANAGER_CONTEXT_MENU_CLEARALL");
    menuItem5.Index = 4;
    menuItem5.Text = resourceManager.GetString("MOVIE_MANAGER_CONTEXT_MENU_CLEARCHILDSONLY");
    menuItem1.Click += new EventHandler(this.menuItemMarkAll_Click);
    menuItem2.Click += new EventHandler(this.menuItemMarkOnly_Click);
    menuItem3.Click += new EventHandler(this.menuItemMarkChildsOnly_Click);
    menuItem4.Click += new EventHandler(this.menuItemClearAll_Click);
    menuItem5.Click += new EventHandler(this.menuItemClearChildsOnly_Click);
    ((Control) this).ContextMenu = this.m_contextMenuMarkByLavel;
  }

  private void menuItemMarkAll_Click(object sender, EventArgs e) => this.MarkAll();

  private void menuItemMarkOnly_Click(object sender, EventArgs e) => this.MarkOnly();

  private void menuItemMarkChildsOnly_Click(object sender, EventArgs e) => this.MarkChildsOnly();

  private void menuItemClearAll_Click(object sender, EventArgs e) => this.ClearAll();

  private void menuItemClearChildsOnly_Click(object sender, EventArgs e) => this.ClearChildsOnly();

  public void MarkAll()
  {
    if (this.m_slelectedRowDataInProcessTree.Count > 0)
    {
      ArrayList arrayList = new ArrayList();
      foreach (CApMovMovieManagerNodeData movieManagerNodeData in this.m_slelectedRowDataInProcessTree)
      {
        arrayList.Add((object) movieManagerNodeData);
        arrayList.AddRange((ICollection) this.GetChildsNodeData(movieManagerNodeData.RowIndex));
      }
      foreach (CApMovMovieManagerNodeData movieManagerNodeData in arrayList)
      {
        if (movieManagerNodeData.CanAttachNewObject)
        {
          ((C1FlexGridBase) this).SetCellCheck(movieManagerNodeData.RowIndex, 1, (CheckEnum) 1);
          movieManagerNodeData.IsNodeOperationSelected = true;
        }
      }
    }
    else
    {
      foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
      {
        if (((RowCol) row).Index > 0 && ((RowCol) row).UserData is CApMovMovieManagerNodeData userData && userData.CanAttachNewObject)
        {
          ((C1FlexGridBase) this).SetCellCheck(((RowCol) row).Index, 1, (CheckEnum) 1);
          userData.IsNodeOperationSelected = true;
        }
      }
    }
  }

  public void MarkByLevel(int selectedLevel)
  {
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).UserData is CApMovMovieManagerNodeData userData && userData.CanAttachNewObject && userData.NodeLevel == selectedLevel)
      {
        ((C1FlexGridBase) this).SetCellCheck(((RowCol) row).Index, 1, (CheckEnum) 1);
        userData.IsNodeOperationSelected = true;
      }
    }
    this.ExpandTreeToCurrentLavel(selectedLevel);
  }

  public void MarkOnly()
  {
    if (this.m_slelectedRowDataInProcessTree.Count <= 0)
      return;
    foreach (CApMovMovieManagerNodeData movieManagerNodeData in this.m_slelectedRowDataInProcessTree)
    {
      if (movieManagerNodeData.CanAttachNewObject)
      {
        ((C1FlexGridBase) this).SetCellCheck(movieManagerNodeData.RowIndex, 1, (CheckEnum) 1);
        movieManagerNodeData.IsNodeOperationSelected = true;
      }
    }
  }

  public void MarkChildsOnly()
  {
    ArrayList arrayList = new ArrayList();
    if (this.m_slelectedRowDataInProcessTree.Count <= 0)
      return;
    foreach (CApMovMovieManagerNodeData movieManagerNodeData in this.m_slelectedRowDataInProcessTree)
      arrayList.AddRange((ICollection) this.GetChildsNodeData(movieManagerNodeData.RowIndex));
    foreach (CApMovMovieManagerNodeData movieManagerNodeData in arrayList)
    {
      if (movieManagerNodeData.CanAttachNewObject)
      {
        ((C1FlexGridBase) this).SetCellCheck(movieManagerNodeData.RowIndex, 1, (CheckEnum) 1);
        movieManagerNodeData.IsNodeOperationSelected = true;
      }
    }
  }

  public void ClearChildsOnly()
  {
    ArrayList arrayList = new ArrayList();
    if (this.m_slelectedRowDataInProcessTree.Count <= 0)
      return;
    foreach (CApMovMovieManagerNodeData movieManagerNodeData in this.m_slelectedRowDataInProcessTree)
      arrayList.AddRange((ICollection) this.GetChildsNodeData(movieManagerNodeData.RowIndex));
    foreach (CApMovMovieManagerNodeData movieManagerNodeData in arrayList)
    {
      if (movieManagerNodeData.CanAttachNewObject)
      {
        ((C1FlexGridBase) this).SetCellCheck(movieManagerNodeData.RowIndex, 1, (CheckEnum) 2);
        movieManagerNodeData.IsNodeOperationSelected = false;
      }
    }
  }

  public void MarkLinesWithoutMovies()
  {
    int num = -1;
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).UserData is CApMovMovieManagerNodeData userData && userData.CanAttachNewObject && !userData.IsNodeHasAVIAttachments)
      {
        ((C1FlexGridBase) this).SetCellCheck(((RowCol) row).Index, 1, (CheckEnum) 1);
        num = num < userData.NodeLevel ? userData.NodeLevel : num;
        userData.IsNodeOperationSelected = true;
      }
    }
    if (num < 0)
      return;
    ((C1FlexGridBase) this).Tree.Show(num);
  }

  public void ClearAll()
  {
    if (this.m_slelectedRowDataInProcessTree.Count > 0)
    {
      ArrayList arrayList = new ArrayList();
      foreach (CApMovMovieManagerNodeData movieManagerNodeData in this.m_slelectedRowDataInProcessTree)
      {
        arrayList.Add((object) movieManagerNodeData);
        arrayList.AddRange((ICollection) this.GetChildsNodeData(movieManagerNodeData.RowIndex));
      }
      foreach (CApMovMovieManagerNodeData movieManagerNodeData in arrayList)
      {
        if (movieManagerNodeData.CanAttachNewObject)
        {
          ((C1FlexGridBase) this).SetCellCheck(movieManagerNodeData.RowIndex, 1, (CheckEnum) 2);
          movieManagerNodeData.IsNodeOperationSelected = false;
        }
      }
    }
    else
    {
      foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
      {
        if (((RowCol) row).Index > 0 && ((RowCol) row).UserData is CApMovMovieManagerNodeData userData && userData.CanAttachNewObject)
        {
          ((C1FlexGridBase) this).SetCellCheck(((RowCol) row).Index, 1, (CheckEnum) 2);
          userData.IsNodeOperationSelected = false;
        }
      }
    }
  }

  public void ClearProcessTreeSelection()
  {
    if (((C1FlexGridBase) this).RowSel <= 0)
      return;
    ((ScrollableControl) this).BeginUpdate();
    ((C1FlexGridBase) this).Select(-1, -1);
    ((C1FlexGridBase) this).AutoSizeCols(0, 2, 1);
    ((C1FlexGridBase) this).AutoSizeRows();
    ((ScrollableControl) this).EndUpdate();
  }

  public void FrozeProcessTree(bool shouldFroze)
  {
    if (shouldFroze)
      ((ScrollableControl) this).BeginUpdate();
    else
      ((ScrollableControl) this).EndUpdate();
  }

  private void LoadTreeData(ArrayList nodeOperationsList)
  {
    int num = 1;
    ((RowColCollection) ((C1FlexGridBase) this).Rows).Count = 1;
    RowCollection rows = ((C1FlexGridBase) this).Rows;
    foreach (CApMovMovieManagerNodeData nodeOperations in nodeOperationsList)
    {
      rows.InsertNode(num, nodeOperations.NodeLevel);
      ((C1FlexGridBase) this).Rows[num].Node.Key = (object) nodeOperations;
      ((C1FlexGridBase) this)[num, 0] = (object) ((ITxObject) nodeOperations.NodeObject).Name;
      ((C1FlexGridBase) this).SetCellImage(num, 0, (Image) nodeOperations.NodeIcon);
      ((C1FlexGridBase) this).SetCellCheck(num, 1, nodeOperations.IsNodeOperationSelected ? (CheckEnum) 1 : (CheckEnum) 2);
      if (!nodeOperations.CanAttachNewObject)
        ((C1FlexGridBase) this).SetCellCheck(num, 1, (CheckEnum) 5);
      if (nodeOperations.IsNodeHasAVIAttachments)
        ((C1FlexGridBase) this).SetCellImage(num, 2, (Image) this.GetAviFileicon());
      nodeOperations.RowIndex = num;
      ((RowCol) rows[num]).AllowEditing = nodeOperations.CanAttachNewObject;
      if (!nodeOperations.CanAttachNewObject)
        ((C1FlexGridBase) this).SetCellStyle(num, 1, this.m_csGrayedCenterItem);
      ++num;
    }
  }

  private Bitmap GetAviFileicon()
  {
    Bitmap aviFileicon = (Bitmap) null;
    try
    {
      aviFileicon = new Bitmap(this.m_imageListAVIIcon.Images[0]);
    }
    catch
    {
    }
    return aviFileicon;
  }

  private void UpdateSelectedRows()
  {
    this.m_slelectedRowDataInProcessTree.Clear();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).Selected && ((RowCol) row).UserData is CApMovMovieManagerNodeData userData)
        this.m_slelectedRowDataInProcessTree.Add((object) userData);
    }
  }

  public void StoreMarkedActivityesStates()
  {
    this.GetResourceManager();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this).Rows)
    {
      if (((RowCol) row).Index > 0 && ((RowCol) row).UserData is CApMovMovieManagerNodeData userData && userData.CanAttachNewObject)
        userData.StoreMarkedActivityesStates();
    }
  }

  private CApMovMovieManagerNodeData GetParentNodeData(int rowIndex)
  {
    return ((RowCol) ((C1FlexGridBase) this).Rows[rowIndex].Node.GetNode((NodeTypeEnum) 1).Row).UserData as CApMovMovieManagerNodeData;
  }

  private ArrayList GetChildsNodeData(int rowIndex)
  {
    ArrayList childsNodeData = new ArrayList();
    RowCollection rows = ((C1FlexGridBase) this).Rows;
    CellRange cellRange = rows[rowIndex].Node.GetCellRange();
    int num = ((CellRange) ref cellRange).TopRow + 1;
    int bottomRow = ((CellRange) ref cellRange).BottomRow;
    if (num <= bottomRow && num > 0 && bottomRow < ((RowColCollection) rows).Count)
    {
      for (int index = num; index <= bottomRow; ++index)
      {
        if (((RowCol) rows[index]).UserData is CApMovMovieManagerNodeData userData)
          childsNodeData.Add((object) userData);
      }
    }
    return childsNodeData;
  }

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.StringTable", ((object) this).GetType().Assembly);
  }
}
